export type CanvasMethod =
  | 'getCanvas'
  | 'setCanvas'
  | 'clearCanvas'
  | 'getNodes'
  | 'addNodes'
  | 'updateNodes'
  | 'moveNode'
  | 'moveNodeRecord'
  | 'changeNodeSize'
  | 'changeNodeSizeRecord'
  | 'changeNodePort'
  | 'deleteNodes'
  | 'addEdges'
  | 'getEdges'
  | 'updateEdges'
  | 'deleteEdges'
  | 'getCanvasProperties'
  | 'setCanvasProperties'
  | 'selectNodesOrEdges'
  | 'getSelectedNodesOrEdges'
  | 'readHistory'

export type CallerSourceType = 'canvas2D' | 'property' | 'mainProcess' | 'resourceMana'

export interface CanvasType {
  nodes: Node[]
  edges: Edge[]
  canvasProperties: CanvasProperties
}

export interface Node {
  id: string
  x: number
  y: number
  width: number
  height: number
  rotate: number
  shape: string
  ports: Port[]
  name: string
  type: string
  points?: string
  zIndex?: number
  style: {
    backgroundColor?: string
    borderWidth?: number
    borderType?: 'solid' | 'dashed' | 'dotted'
    borderColor?: string
    borderRadius?: number
    icon?: string
    iconSize?: number
    portTop?: PortType
    portRight?: PortType
    portBottom?: PortType
    portLeft?: PortType
  }
  simData: {
    // 产品样式
    product?: {
      width: number
      height: number
      backgroundColor: string
      borderRadius: number
      borderWidth: number
      borderColor: string
    }

    // 发生器
    produce?: {
      quantity: number
      interval: {
        type: 'fixed' | 'random'
        fixed: number
        random: string
      }
      selfCirculation: boolean
    }

    //加工站
    process?: {
      processTime: {
        type: 'fixed' | 'random'
        fixed: number
        random: string
      }
      //不良品率
      defectiveRate: number
      //故障率
      failureRate: number
      //是否开启上下料点
      enablePorts: boolean
      //上料点
      loaderPoints: [number, number]
      //下料点
      cuttingPoints: [number, number]
      //人员协助
      personAssist: boolean
    }

    //批处理
    batchProcess?: {
      batchQuantity: number
      processTime: number
      animationDirection: 'left' | 'right' | 'up' | 'down'
    }

    //缓冲区
    buffer?: {
      capacity: number
    }

    // 装配站
    assemble?: {
      assembleTime: number
      inputPorts: Record<string, number>
      inputPortsVisual: { id: string; name: string; value: number }[]
    }

    //拆卸站
    disassemble?: {
      disassembleTime: number
      quantity: number
    }

    //AGV
    agv?: {
      speed: number
      capacity: number
      rotateSpeed: number
      loadTime: number
      unloadTime: number
      enableTraffic: boolean
    }

    //工人池
    workerPool?: {
      workerNum: number
      workerSpeed: number
      maintenanceTime: number
      associatedDevices: { id: string; name: string }[]
    }

    //自定义文本
    plaintext?: {
      text: string
      fill: string
      fontSize: number
      fontFamily: string
      fontWeight: string
      letterSpacing: number
    }
  }
}

export interface Edge {
  id: string
  shape: string
  name: string
  sourceId: string | { x: number; y: number }
  sourcePortId: string | undefined
  targetId: string | { x: number; y: number }
  targetPortId: string | undefined
  zIndex?: number
  vertices?: { x: number; y: number }[]
  labels?: {
    text: string
    distance: number
  }[]
  style?: {
    width: number
    router: 'normal' | 'orth'
    connectorName: 'rounded' | 'normal' | 'smooth'
    connectorRadius: number
  }
  simData?: {
    path?: string
    isTransport?: boolean | string
    transportId?: string
    belt?: {
      speed: number
    }
  }
}

export interface CanvasProperties {
  background: string
  showGrid: boolean
  gridColor: string
  selectedNodeStyle: 'rotate' | 'ripple' | 'mask'
  selectedNodeStyleColor: string
  lock: boolean
  enableSnapline: boolean
}

export interface Port {
  id: string
  group: string
}

export interface PortType {
  id: string
  type: 'input' | 'output' | 'twoway' | 'none'
}

export const CanvasBehaviorActionList = [
  'addNodes',
  'addEdges',
  'deleteNodes',
  'deleteEdges',
  'moveNodeRecord',
  'changeNodeSizeRecord'
] as const
export type CanvasBehaviorActionType = (typeof CanvasBehaviorActionList)[number]
export interface CanvasBehaviorHistory {
  action: CanvasBehaviorActionType
  data: { id: string; x?: number; y?: number; width?: number; height?: number; other?: any }[]
}
